FinishTemplateForm = function(config) {

    // Privatne varijable
    var privateVariable = '';

    this.valueData = {
    };

    this.formPanel = new Ext.form.FormPanel({
        id: 'finishTemplateFormId',
        baseCls: 'x-plain',
        fileUpload : true,
        border:true,
        defaults: {
            anchor: '95%',
            msgTarget: 'side'
        },
        items : [ {
                     xtype: 'button',
                     fieldLabel: 'Trenutna verzija',
                     anchor: '0%',
                     text: 'Download',
                     handler : function() {
                        var url = this.formPanel.getForm().findField('packageDownloadUrl').getValue();
                        window.open(url);
                     },
                     scope : this
                 },
                 {
                     xtype: 'fileuploadfield',
                     emptyText: 'Odaberi datoteku...',
                     fieldLabel: 'Nova verzija',
                     name: 'fileData',
                     buttonText: '',
                     buttonCfg: {
                         iconCls: 'button-browseFile'
                     }
                 },
                 {
                     xtype : 'hidden',
                     name : 'packageDownloadUrl'
                 }, {
                     xtype : 'hidden',
                     name : 'packageId'
                 }, {
                     xtype : 'hidden',
                     name : 'taskId'
                 }
        ],
        buttons: [ {
                text : 'Odustani',
                handler :function(){
                    this.close();
                },
                scope : this
            }, {
                text : 'Upload',
                handler: function() {
                    var fileField = this.formPanel.getForm().findField('fileData');
                    if (fileField.getValue() === undefined || fileField.getValue() === '') {
                        fileField.markInvalid("Ovo polje je obavezno!");
                    }
                    else {
                        this.formSubmit(function(){
                            this.formPanel.getForm().findField('fileData').reset();
                            MessageBoxHandler.showInfoBox('Datoteka uspješno uploadana.');
                        });
                    }
                },
                scope : this
            }, {
                text : 'Završi',
                handler : function(){
                    var fileField = this.formPanel.getForm().findField('fileData');
                    if (fileField.getValue() === undefined || fileField.getValue() === '') {
                        var question = 'Niste dodali novu verziju predloška. Jeste li sigurni da želite postojeću verziju poslati na odobrenje?';
                        Ext.MessageBox.confirm('Confirm', question, function(btn){
                            if (btn === 'yes') {
                                this.formSubmit({taskAction:'finish'},function(){this.close();});
                            }
                        }, this);
                    }
                    else {
                        this.formSubmit({taskAction:'finish'},function(){this.close();});
                    }
                },
                scope : this
            }, {
                text : 'Obriši',
                handler :function(){
                    var question = 'Jeste li sigurni da želite obrisati predložak?';
                    Ext.MessageBox.confirm('Confirm', question, function(btn){
                        if (btn === 'yes') {
                            this.formSubmit({taskAction:'delete'},function(){this.close();});
                        }
                    }, this);
                },
                scope : this
            }
       ]
    });

    Ext.apply(this, config);

    // Defaultna konfiguracija
    Ext.apply(this, {
        id: 'finishTemplateWindowId'
            ,title: 'Završi uređivanje predloška'
            ,autoHeight:true
            //,autoWidtht:true
            ,width:400
            ,bodyStyle:'padding:5px;'
            ,buttonAlign:'center'
            ,modal:true
            ,stateful:false
            ,border:true
            ,items:[this.formPanel]
            ,keys: [{
              key: Ext.EventObject.ENTER,
              fn: function(){},
              scope:this
            }]
        });

    FinishTemplateForm.superclass.constructor.call(this);

    this.on('beforerender', function(formPanel) {
        this.formPanel.getForm().setValues(formPanel.valueData);
    }, this);

};

Ext.extend(FinishTemplateForm, Ext.Window, {

    formSubmit : function(params, successCallback){
        this.formPanel.getForm().submit( {
            url : 'mvc/document',
            method : 'POST',
            scope : this,
            params : params,
            success : successCallback,
            failure : function(){alert("Greška!");},
            waitTitle : 'Molim pričekajte',
            waitMsg : 'Spremam!',
            submitEmptyText : false,
            clientValidation : false
        });
    }

});

Ext.reg('finishTemplateForm', FinishTemplateForm);